package com.ncmmall.application.business.live;

import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.goods.QGoods;
import com.ncmmall.domain.business.live.LiveGoods;
import com.ncmmall.domain.business.live.LiveGoodsAnchor;
import com.ncmmall.domain.business.live.LiveGoodsAnchorRepository;
import com.ncmmall.domain.business.live.LiveGoodsRepository;
import com.ncmmall.domain.business.live.LiveSessionGoods;
import com.ncmmall.domain.business.live.LiveSessionGoodsRepository;
import com.ncmmall.domain.business.live.LiveSessionRedpacket;
import com.ncmmall.domain.business.live.LiveSessionRedpacketRepository;
import com.ncmmall.domain.business.live.LiveSessionUserRedPacket;
import com.ncmmall.domain.business.live.LiveSessionUserRedpacketRepository;
import com.ncmmall.domain.business.live.QLiveGoodsAnchor;
import com.ncmmall.domain.business.live.QLiveSessionGoods;
import com.ncmmall.domain.business.live.QLiveSessionRedpacket;
import com.ncmmall.domain.business.live.QLiveSessionUserRedPacket;
import com.ncmmall.domain.security.user.QWebUser;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class LiveGoodsApplication {

    @Autowired
    private LiveGoodsRepository liveGoodsRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private LiveGoodsAnchorRepository liveGoodsAnchorRepository;

    @Autowired
    private LiveSessionGoodsRepository liveSessionGoodsRepository;

    @Autowired
    private LiveSessionUserRedpacketRepository liveSessionUserRedpacketRepository;

    @Autowired
    private LiveSessionRedpacketRepository liveSessionRedpacketRepository;

    public void remove(LiveGoods liveGoods) {
//        liveGoods.setStatus(3);
//        liveGoodsRepository.save(liveGoods);
//
        List<LiveGoodsAnchor> liveGoodsAnchorList = queryChannelService.findAll(QLiveGoodsAnchor.liveGoodsAnchor.liveGoods.id.eq(liveGoods.getId()), LiveGoodsAnchor.class);
//        for (LiveGoodsAnchor liveGoodsAnchor : liveGoodsAnchorList) {
//            liveGoodsAnchor.setStatus(2);
//            liveGoodsAnchorRepository.save(liveGoodsAnchor);
//        }


        List<LiveSessionGoods> liveSessionGoods = queryChannelService.findAll(QLiveSessionGoods.liveSessionGoods.liveGoodsAnchor.in(liveGoodsAnchorList), LiveSessionGoods.class);
        List<LiveSessionRedpacket> liveSessionRedpackets = queryChannelService.findAll(QLiveSessionRedpacket.liveSessionRedpacket.liveSessionGoods.in(liveSessionGoods), LiveSessionRedpacket.class);
        List<LiveSessionUserRedPacket> liveSessionUserRedPackets = queryChannelService.findAll(QLiveSessionUserRedPacket.liveSessionUserRedPacket.liveSessionRedpacket.in(liveSessionRedpackets), LiveSessionUserRedPacket.class);
        liveSessionUserRedpacketRepository.delete(liveSessionUserRedPackets);
        liveSessionRedpacketRepository.delete(liveSessionRedpackets);
        liveSessionGoodsRepository.delete(liveSessionGoods);
        liveGoodsAnchorRepository.delete(liveGoodsAnchorList);
        liveGoodsRepository.delete(liveGoods);
    }

    public void removes(List<LiveGoods> liveGoodsList) {
        for (LiveGoods liveGoods : liveGoodsList) {
            remove(liveGoods);
        }
    }

    public void create(LiveGoods params) {

        List<Long> ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(params.getGoodsIds()).stream().map(Long::valueOf).collect(Collectors.toList());
        List<Goods> goodsList = queryChannelService.findAll(QGoods.goods.id.in(ids), Goods.class);

        for (Goods goods : goodsList) {

            LiveGoods liveGoods = new LiveGoods()
                    .setMerchant(goods.getMerchant())
                    .setGoods(goods)
                    .setLiveScope(params.getLiveScope())
                    .setCreateTime(new Timestamp(System.currentTimeMillis()))
                    .setGuaranteeScale(params.getGuaranteeScale())
                    .setStatus(1);
            liveGoodsRepository.save(liveGoods);

            List<WebUser> users;
            if (params.getLiveScope() == 2) {
                ids = Splitter.on(";").omitEmptyStrings().trimResults().splitToList(params.getDistributorIds()).stream().map(Long::valueOf).collect(Collectors.toList());
                users = queryChannelService.findAll(QWebUser.webUser.id.in(ids), WebUser.class);
            } else {
                users = queryChannelService.queryAllEntity("select * from security_web_user where liveType = 1", Collections.emptyMap(), WebUser.class);
            }
            for (WebUser user : users) {
                LiveGoodsAnchor liveGoodsAnchor = new LiveGoodsAnchor()
                        .setAnchor(user)
                        .setLiveGoods(liveGoods)
                        .setGoods(liveGoods.getGoods());
                liveGoodsAnchorRepository.save(liveGoodsAnchor);
            }
        }
    }

    public void disable(LiveGoods liveGoods) {
        liveGoods.setStatus(2);
        liveGoodsRepository.save(liveGoods);
    }

    public void enable(LiveGoods liveGoods) {
        liveGoods.setStatus(1);
        liveGoodsRepository.save(liveGoods);
    }

    /**
     * 编辑排序值
     */
    public void modifyRank(LiveGoods liveGoods) {
        LiveGoods goods = queryChannelService.findOne(LiveGoods.class, liveGoods.getId());
        goods.setRank(liveGoods.getRank());
        liveGoodsRepository.save(goods);
    }
}
